﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Spiral
{
   class Spiral
   {
      static void Main(string[] args)
      {
         Console.Write("Please enter 'n' number: ");
         string readN = Console.ReadLine();
         int n = 0;
         bool isNValid = int.TryParse(readN, out n);
         int[,] spiralMatrix = new int[n, n];

         if (isNValid)
         {
            int counter = 1;
            int row = 0;
            int col = 0;
            int top = 0;
            int bottom = 0;

            while (counter <= (n * n))
            {
               if (counter > (n * n))
               {
                  break;
               }

               for (int i = col; i < (n - top); i++)
               {
                  spiralMatrix[row, col] = counter;
                  counter++;
                  col++;
               }

               if (counter > (n * n))
               {
                  break;
               }

               col--;
               row++;

               for (int i = row; i < (n - top); i++)
               {
                  spiralMatrix[row, col] = counter;
                  counter++;
                  row++;
               }

               if (counter > (n * n))
               {
                  break;
               }

               row--;
               col--;

               for (int i = col; i >= bottom; i--)
               {
                  spiralMatrix[row, col] = counter;
                  counter++;
                  col--;
               }

               if (counter > (n * n))
               {
                  break;
               }

               row--;
               col++;

               for (int i = row; i > bottom; i--)
               {
                  spiralMatrix[row, col] = counter;
                  counter++;
                  row--;
               }

               row++;
               col++;

               top++;
               bottom++;
            }

            for (int i = 0; i < n; i++)
            {
               for (int j = 0; j < n; j++)
               {
                  Console.Write("{0,3}", spiralMatrix[i, j]);
               }

               Console.WriteLine();
            }

            Console.WriteLine();
         }
      }
   }
}
